<?php
/**
 * Rooms Model for RoomBooking Component
 * 
 * @version    1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license	   GNU/GPL
 */
 
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
 
jimport( 'joomla.application.component.model' );
 
class RoomBookingModelRooms extends JModel
{
    /**
     * Rooms data array
     *
     * @var array
     */
    var $_data;
 
    /**
     * Returns the query
     * @return string The query to be used to retrieve the rows from the database
     */
    function _buildQuery()
    {
        $filter_type = JRequest::getVar('filter_type');
//        echo $filter_type;
        $like = '';
        if($filter_type != null && strcasecmp($filter_type,'0'))$like = " WHERE t.name LIKE '%".$filter_type."%' ";
//        echo $like;
        $query = 'SELECT t.name as room_type,r.room_type_id, r.id, r.name, r.floor, r.open, b.name as building_name, r.id_building FROM #__roombooking_rooms AS r '. 
				 ' LEFT OUTER JOIN (#__roombooking_buildings AS b,#__roombooking_rooms_types as t) ON (r.id_building = b.id AND r.room_type_id = t.id)'.$like.' ORDER BY id';
//        echo $query;
        return $query;
    }
 
    /**
     * Retrieves the room data
     * @return array Array of objects containing the data from the database
     */
    function getData()
    {
        // Lets load the data if it doesn't already exist
        if (empty( $this->_data ))
        {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList( $query );
        }
 
        return $this->_data;
    }
    
	/**
	 * Method to get open rooms
	 * @return object with data
	 */
	function getRooms()
	{
		// Load the data
		$query = ' SELECT * FROM #__roombooking_rooms WHERE open = 1' ;
		$rooms = $this->_getList( $query );
			
		return $rooms;
	}
}
